package com.example.big_job;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.big_job.DB.DBHelper;
//import com.example.big_job.user.UserManager;

public class RegisterActivity extends AppCompatActivity {
    private Button B_register;
    private EditText ET_user;
    private EditText ET_pwd1;
    private EditText ET_pwd2;
    private Button B_fanhui;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);



        //找到控件
        B_register=findViewById(R.id.bt_r);
        ET_user=findViewById(R.id.et_u);
        ET_pwd1=findViewById(R.id.et_p1);
        ET_pwd2=findViewById(R.id.et_p2);
        B_fanhui=findViewById(R.id.bt_r_r);

        //返回登陆
        B_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                intent=new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
//                DBHelper sqlite = new DBHelper(RegisterActivity.this,1);
//                SQLiteDatabase database =sqlite.getReadableDatabase();
//                database.delete("user", "password=?", new String[]{""});
            }
        });

        //跳转到登陆页面
        B_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username=ET_user.getText().toString();
                String pwd1=ET_pwd1.getText().toString().trim();
                String pwd2=ET_pwd2.getText().toString().trim();

                Intent intent = null;
                DBHelper sqlite = new DBHelper(RegisterActivity.this,1);
                SQLiteDatabase database =sqlite.getReadableDatabase();
                Cursor Rawcursor = database.rawQuery("select * from user where userName=?", new String[]{username});
                if(username.equals("")){
                    Toast.makeText(getApplicationContext(), "用户名不能为空！", Toast.LENGTH_SHORT).show();
                }else
                    if(Rawcursor.getCount()>0){
                    Toast.makeText(getApplicationContext(), "该用户名已经被注册", Toast.LENGTH_SHORT).show();
                    Log.i("TAG",username+" 已经存在！");
                }
                else if(pwd1.equals("")){
                    Toast.makeText(getApplicationContext(), "密码不能为空！", Toast.LENGTH_SHORT).show();
                }else if(!pwd2.equals(pwd1)){
                    Toast.makeText(getApplicationContext(), "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "注册成功！", Toast.LENGTH_SHORT).show();
                    intent=new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    Log.i("TAG",username+"   "+pwd1);
                    ContentValues values = new ContentValues();
                    values.put("username",username);
                    values.put("password",pwd1);
                    database.insert("user",null,values);
                }


            }
        });



    }
}